//
//  RHLocalStatisticsCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/27.
//

#import "RHLocalStatisticsCell.h"
@interface RHLocalStatisticsCell()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic ,strong) QMUILabel *feedLab;

@property (nonatomic ,strong) QMUILabel *batteryLab;

@property (nonatomic ,strong) QMUILabel *powerLab;

@property (nonatomic ,strong) QMUILabel *loadLab;

@property (nonatomic ,strong) QMUILabel *solarLab;
//电压
@property (nonatomic ,strong) QMUILabel *volLab;
//电流
@property (nonatomic ,strong) QMUILabel *eletriLab;
//soc
@property (nonatomic ,strong) QMUILabel *socLab;
@end
@implementation RHLocalStatisticsCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setBleDataDic:(NSDictionary *)bleDataDic
{
    if(bleDataDic[@"Emu_TV"]){
        
        NSString *tvStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Emu_TV"] doubleValue]];
        self.volLab.text = [NSString stringWithFormat:@"%@ %@",tvStr,@"V"];
    }
    
    if(bleDataDic[@"Emu_Cur"]){
        NSString *curStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Emu_Cur"] doubleValue]];
        self.eletriLab.text = [NSString stringWithFormat:@"%@ %@",curStr,@"A"];
    }
    
    if(bleDataDic[@"Emu_SOC"]){
        NSString *socStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Emu_SOC"] doubleValue]];
        self.socLab.text = [NSString stringWithFormat:@"%@ %@",socStr,@"%"];
    }
    
    if(bleDataDic[@"Ems_PvPwr"]){
        NSString *pvpwrStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Ems_PvPwr"] doubleValue]];
        self.solarLab.attributedText = [self attributeStringWithValue:pvpwrStr unit:@" (kW)"];
    }
    
    if(bleDataDic[@"Ems_GridPwr"]){
        NSString *gridStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Ems_GridPwr"] doubleValue]];
        self.feedLab.attributedText = [self attributeStringWithValue:gridStr unit:@" (kW)"];
    }
    
    if(bleDataDic[@"Ems_LoadPwr"]){
        NSString *loadStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Ems_LoadPwr"] doubleValue]];
        self.loadLab.attributedText = [self attributeStringWithValue:loadStr unit:@" (kW)"];
    }
    
    if(bleDataDic[@"Ems_BatPwr"]){
        NSString *batStr = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[bleDataDic[@"Ems_BatPwr"] doubleValue]];
        self.batteryLab.attributedText = [self attributeStringWithValue:batStr unit:@" (kW)"];
    }
    
    
}

-(NSMutableAttributedString*)attributeStringWithValue:(NSString*)value unit:(NSString*)unitStr
{
    
    NSMutableAttributedString *mtbAtriStr = [[NSMutableAttributedString alloc]init];
    NSAttributedString *atrStr_value = [[NSAttributedString alloc]initWithString:value attributes:@{NSFontAttributeName:kSYSTEMFONT(16),NSForegroundColorAttributeName:kRGB(63, 68, 67)}];
    NSAttributedString *atrStr_unit = [[NSAttributedString alloc]initWithString:unitStr attributes:@{NSFontAttributeName:kSYSTEMFONT(12),NSForegroundColorAttributeName:kColorRGBA(63, 68, 67, 0.7)}];
    [mtbAtriStr appendAttributedString:atrStr_value];
    [mtbAtriStr appendAttributedString:atrStr_unit];
    return mtbAtriStr;
}

-(void)setupUI
{
    UIView *bgView = kView;
    bgView.backgroundColor = CFTabViewBgColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    NSArray *titles;
    NSArray *icons;
    NSInteger row = 3;
    CGFloat itemHeight = kScaleW(70);
    if([self.reuseIdentifier isEqualToString:@"RHLocalStatisticsCell_top"]){
        titles = @[@"电压".rh_localized,@"电流".rh_localized,@"SOC".rh_localized];
        icons = @[@"device_local_vol",@"device_local_ele",@"device_local_soc"];
    }else{
        titles = @[@"负载".rh_localized,@"电池".rh_localized,@"电网".rh_localized,@"光伏".rh_localized];
        icons = @[@"local_statistics_load",@"local_statistics_battery",@"local_statistics_grid",@"local_statistics_solar"];
        row = 2;
        itemHeight = kScaleW(64);
    }
    CGFloat itemWidth = (kUI_WINDOW_W - 30 - (row-1)*10)/row;
    
    
    __block UIView *lastView;
    [titles enumerateObjectsUsingBlock:^(NSString  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [self itemViewWithTitles:titles[idx] images:icons[idx] index:idx];
        itemView.backgroundColor = UIColor.whiteColor;
        itemView.layer.cornerRadius = 8;
        [bgView addSubview:itemView];
        if(!lastView){
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(10);
                make.width.offset(itemWidth);
                make.height.offset(itemHeight);
            }];
        }else{
            if(idx%row==0){
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastView);
                    make.top.equalTo(lastView.mas_bottom).offset(10);
                    make.left.mas_offset(15);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastView);
                    make.top.equalTo(lastView);
                    make.left.equalTo(lastView.mas_right).offset(10);
                }];
            }
            
        }
        lastView = itemView;
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-10);
    }];
    
   
}

-(UIView*)itemViewWithTitles:(NSString*)title images:(NSString *)icon index:(NSInteger)idx
{
    UIView *itemView = kView;
    UIImageView* iconImg = [[UIImageView alloc]init];
    iconImg.image = [UIImage imageNamed:icon];
    [itemView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kScaleW(25));
        make.top.mas_offset(10);
        make.left.mas_offset(15);
    }];
    
   QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.tag = 100+idx;
    contentLab.text = @"--";
    contentLab.font = [UIFont boldSystemFontOfSize:16];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.textColor = kCOLOR_HEX(@"3F4443");
    contentLab.adjustsFontSizeToFitWidth = YES;
    contentLab.minimumScaleFactor = 0.5;
    [itemView addSubview:contentLab];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = title;
    titleLab.font = [UIFont systemFontOfSize:13];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.textColor = kCOLOR_HEX(@"3F4443");
    titleLab.numberOfLines = 2;
    [itemView addSubview:titleLab];
    
    kWeakSelf;
    if([self.reuseIdentifier isEqualToString:@"RHLocalStatisticsCell_top"])
    {
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImg.mas_right).offset(kScaleW(10));
            make.top.mas_offset(15);
            make.right.mas_offset(-10);
        }];
        
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.bottom.mas_offset(-15);
            make.right.mas_offset(-10);
        }];
    }else{
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImg.mas_right).offset(kScaleW(10));
            make.top.mas_offset(15);
            make.right.mas_offset(-3);
        }];
        
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentLab);
            make.bottom.mas_offset(-15);
            make.right.mas_offset(-10);
        }];
    }
    
    if([self.reuseIdentifier isEqualToString:@"RHLocalStatisticsCell_top"]) {
        if(idx == 0) {self.volLab = contentLab;}
        if(idx == 1) {self.eletriLab = contentLab;}
        if(idx == 2) {self.socLab = contentLab;}
    }
    
    if([self.reuseIdentifier isEqualToString:@"RHLocalStatisticsCell_bottom"]) {
        if(idx == 0) {self.loadLab = contentLab;}
        if(idx == 1) {self.batteryLab = contentLab;}
        if(idx == 2) {self.feedLab = contentLab;}
        if(idx == 3) {self.solarLab = contentLab;}
    }
    
    return itemView;
}

@end
